<!DOCTYPE html>
<html>

<head>
    <title>custom-pixel-altitude</title>
    <script type="text/javascript" src="https://unpkg.com/dat.gui@0.7.6/build/dat.gui.min.js"></script>
    <link type="text/css" rel="stylesheet" href="https://unpkg.com/maptalks/dist/maptalks.css">
    <script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.js"></script>
    <script type="text/javascript" src="https://unpkg.com/three@0.138.0/build/three.min.js"></script>
    <!--fatline script-->
    <!-- https://threejs.org/examples/#webgl_lines_fat -->
    <script type="text/javascript"
        src="https://unpkg.com/three@0.138.0/examples/js/lines/LineMaterial.js"></script>
    <!---->
    <script type="text/javascript" src="https://unpkg.com/lz-string@1.4.4/libs/lz-string.min.js"></script>
    <!-- <script type="text/javascript" src="./https://unpkg.com/maptalks.three@latest/dist/maptalks.three.js"></script> -->
    <script type="text/javascript" src="https://unpkg.com/maptalks.three@latest/dist/maptalks.three.js"></script>
    <script type="text/javascript" src="./js/geoutil.js"></script>
    <script type="text/javascript"
        src="https://unpkg.com/three@0.138.0/examples/js/libs/stats.min.js"></script>
    <style>
        html,
        body {
            margin: 0px;
            height: 100%;
            width: 100%;
        }

        #map {
            width: 100%;
            height: 100%;
            background-color: black;
        }
    </style>
</head>

<body>
    <div id="map" class="map">

    </div>
    <script>

        var map = new maptalks.Map("map", {
            center: [13.416935229170008, 52.529564137540376],
            zoom: 15,
            pitch: 70,
            bearing: 180,

            centerCross: true,
            doubleClickZoom: false,
            baseLayer: new maptalks.TileLayer('tile', {
                urlTemplate: 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
                subdomains: ['a', 'b', 'c', 'd'],
                attribution: '&copy; <a href="http://osm.org">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/">CARTO</a>'
            })
        });


        // the ThreeLayer to draw buildings
        var threeLayer = new maptalks.ThreeLayer('t', {
            forceRenderOnMoving: true,
            forceRenderOnRotating: true
            // animation: true
        });


        threeLayer.prepareToDraw = function (gl, scene, camera) {

            stats = new Stats();
            stats.domElement.style.zIndex = 100;
            document.getElementById('map').appendChild(stats.domElement);

            var light = new THREE.DirectionalLight(0xffffff);
            light.position.set(0, -10, 10).normalize();
            scene.add(light);
            addLines();
        };
        threeLayer.addTo(map);


        var lines;

        //https://threejs.org/examples/#webgl_lines_fat
        var material = new THREE.LineMaterial({
            color: 0x00ffff,
            transparent: true,
            // vertexColors: THREE.VertexColors,
            // side: THREE.BackSide,
            linewidth: 2 // in pixels
            // vertexColors: THREE.VertexColors,
            // dashed: false

        });

        var highlightmaterial = new THREE.LineMaterial({
            linewidth: 4,
            // side: THREE.BackSide,
            color: 'yellow',
            dashSize: 1,
            gapSize: 1,
            dashed: true
            // opacity: 0.8,
        });
        highlightmaterial.defines.USE_DASH = '';

        function addLines() {
            fetch('./data/berlin-railway.geojson').then(function (res) {
                return res.json();
            }).then(function (geojson) {
                var lineStrings = maptalks.GeoJSON.toGeometry(geojson);
                lines = [];
                for (let i = 0, len = 5; i < len; i++) {
                    const alt = i * 10;
                    var line = threeLayer.toFatLines(lineStrings, { altitude: alt, _altitude: alt, minZoom: 9 }, material);
                    line.zoomChange = zoomChange;
                    lines.push(line);
                    // tooltip test
                    // line.setToolTip(line.getId(), {
                    //     showTimeout: 0,
                    //     eventsPropagation: true,
                    //     dx: 10
                    // });

                    // //infowindow test
                    // line.setInfoWindow({
                    //     content: 'hello world,id:' + line.getId(),
                    //     title: 'message',
                    //     animationDuration: 0,
                    //     autoOpenOn: false
                    // });

                    // //event test
                    // ['click', 'mouseout', 'mouseover', 'mousedown', 'mouseup', 'dblclick', 'contextmenu'].forEach(function (eventType) {
                    //     line.on(eventType, function (e) {
                    //         // console.log(e.type, e);
                    //         // console.log(this.getId());
                    //         if (e.type === 'mouseout') {
                    //             this.setSymbol(material);
                    //         }
                    //         if (e.type === 'mouseover') {
                    //             this.setSymbol(highlightmaterial);
                    //         }
                    //     });
                    // });
                }
                threeLayer.addMesh(lines);
                animation();
                initGui();
            })
        }

        function zoomChange() {
            const map = this.getMap();
            if (!map) {
                return;
            }
            const heightPerPixel = map.pixelToDistance(1, 0);
            const alt = this.getOptions()._altitude;
            this.setAltitude(alt * heightPerPixel);
        }

        function animation() {
            // layer animation support Skipping frames
            threeLayer._needsUpdate = !threeLayer._needsUpdate;
            if (threeLayer._needsUpdate && !threeLayer.isRendering()) {
                threeLayer.redraw();
            }
            stats.update();
            requestAnimationFrame(animation);
        }

        function initGui() {
            var params = {
                add: true,
                color: 0x00ffff,
                show: true,
                opacity: 1,
                altitude: 0
            };
            var gui = new dat.GUI();
            gui.add(params, 'add').onChange(function () {
                if (params.add) {
                    threeLayer.addMesh(lines);
                } else {
                    threeLayer.removeMesh(lines);
                }
            });
            gui.addColor(params, 'color').name('line color').onChange(function () {
                material.color.set(params.color);
                lines.forEach(function (mesh) {
                    mesh.setSymbol(material);
                });
            });
            gui.add(params, 'opacity', 0, 1).onChange(function () {
                material.uniforms.opacity.value = (params.opacity);
                lines.forEach(function (mesh) {
                    mesh.setSymbol(material);
                });
            });
            gui.add(params, 'show').onChange(function () {
                lines.forEach(function (mesh) {
                    if (params.show) {
                        mesh.show();
                    } else {
                        mesh.hide();
                    }
                });
            });
            // gui.add(params, 'altitude', 0, 300).onChange(function () {
            //     lines.forEach(function (mesh) {
            //         mesh.setAltitude(params.altitude);
            //     });
            // });
        }
    </script>
</body>

</html>